package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize) {
        //1.创建分页构造器
        Page<Category> pageInfo = new Page<>(page, pageSize);
        //12.创建查询条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        //14.执行分页查询
        categoryService.page(pageInfo, queryWrapper);
        //15,返回在询结果
        return R.success(pageInfo);
    }
    @PostMapping
    public R<String> sava(@RequestBody Category category, HttpServletRequest request){
        log.info("category:{}",category);
        categoryService.save(category);
        return R.success("添加分类成功");
    }
    @DeleteMapping
    public R<String> delete(Long id){
        log.info("删除分类，i为：{}",id);
        categoryService.remove(id);
        return R.success("分类信息删除成功");
    }

    /**
     * 修改信息
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        log.info("修改分类信息：{}",category);
        categoryService.updateById(category);
        return R.success("修改分类信息成功");
    }
//    @GetMapping("/list")
//    public R<List<Category>> list(Category category){
//        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
//        lambdaQueryWrapper.eq(category.getName()!=null,Category::getName,category.getName());
//        lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
//        //
//        List<Category> list = categoryService.list(lambdaQueryWrapper);
//        return R.success(list);
//
//
//    }
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        //条件选择器
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        lambdaQueryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        lambdaQueryWrapper.eq(category.getName() !=null,Category::getName,category.getName());
        //添加排序条件
        lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(lambdaQueryWrapper);
        return R.success(list);
    }
}
